package cn.ac.jaas.isc.vegetable.admin.controller;

import cn.ac.jaas.isc.vegetable.admin.api.dto.SysOauthClientDetailsDTO;
import cn.ac.jaas.isc.vegetable.admin.api.entity.SysOauthClientDetails;
import cn.ac.jaas.isc.vegetable.admin.service.SysOauthClientDetailsService;
import cn.ac.jaas.isc.vegetable.common.core.util.R;
import cn.ac.jaas.isc.vegetable.common.log.annotation.SysLog;
import cn.ac.jaas.isc.vegetable.common.security.annotation.Inner;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author 张高昌
 * @date 2022/4/28 18:08
 * @description: 前端控制器
 */
@RestController
@AllArgsConstructor
@RequestMapping("/client")
@Api(value = "client", tags = "客户端管理模块")
public class SysClientController {

    private final SysOauthClientDetailsService clientDetailsService;

    /**
     * 通过ID查询
     *
     * @param clientId clientId
     * @return SysOauthClientDetails
     */
    @GetMapping("/{clientId}")
    public R getByClientId(@PathVariable String clientId) {
        return R.ok(clientDetailsService
                .list(Wrappers.<SysOauthClientDetails>lambdaQuery().eq(SysOauthClientDetails::getClientId, clientId)));
    }

    /**
     * 简单分页查询
     *
     * @param page                  分页对象
     * @param sysOauthClientDetails 系统终端
     * @return
     */
    @GetMapping("/page")
    public R getOauthClientDetailsPage(Page page, SysOauthClientDetails sysOauthClientDetails) {
        return R.ok(clientDetailsService.queryPage(page, sysOauthClientDetails));
    }

    /**
     * 添加
     *
     * @param clientDetailsDTO 实体
     * @return success/false
     */
    @SysLog("添加终端")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('sys_client_add')")
    public R add(@Valid @RequestBody SysOauthClientDetailsDTO clientDetailsDTO) {
        return R.ok(clientDetailsService.saveClient(clientDetailsDTO));
    }

    /**
     * 删除
     *
     * @param clientId ID
     * @return success/false
     */
    @SysLog("删除终端")
    @DeleteMapping("/{clientId}")
    @PreAuthorize("@pms.hasPermission('sys_client_del')")
    public R removeById(@PathVariable String clientId) {
        return R.ok(clientDetailsService.removeByClientId(clientId));
    }

    /**
     * 编辑
     *
     * @param clientDetailsDTO 实体
     * @return success/false
     */
    @SysLog("编辑终端")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('sys_client_edit')")
    public R update(@Valid @RequestBody SysOauthClientDetailsDTO clientDetailsDTO) {
        return R.ok(clientDetailsService.updateClientById(clientDetailsDTO));
    }

    @Inner(false)
    @GetMapping("/getClientDetailsById/{clientId}")
    public R getClientDetailsById(@PathVariable String clientId) {
        return R.ok(clientDetailsService.getOne(
                Wrappers.<SysOauthClientDetails>lambdaQuery().eq(SysOauthClientDetails::getClientId, clientId), false));
    }

    /**
     * 查询全部客户端
     *
     * @return
     */
    @Inner(false)
    @GetMapping("/list")
    public R listClients() {
        return R.ok(clientDetailsService.list());
    }
}
